package com.sh.data.engine.domain.normalization.modeldesign.model.model.enums;

import com.sh.data.engine.domain.normalization.modeldesign.model.model.domain.StorageDomain;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字段枚举
 *
 * @author : changkong
 * @date : 2022/3/6
 */
public enum PgFieldTypeEnum {
    // pg中没有tinyint类型, 若用户在hive模式下选择tinyint, 则在pg中映射成smallint
    TINYINT("smallint", false),
    SMALLINT("smallint", true),
    INT("integer", true),
    BIGINT("bigint", true),

    FLOAT("real", true),
    DOUBLE("double precision", true),
    DECIMAL("numeric(20,10)", true),

    TIMESTAMP("timestamp without time zone", true),

    CHAR("character(255)", true),
    STRING("text", true),
    VARCHAR("character varying(400)", true),

    DATE("date", true),
    BOOLEAN("boolean", true),
    BINARY("bytea", true);

    private final String dbFieldType;
    // 是否对前台用户可见
    private boolean userVisible;

    PgFieldTypeEnum(String dbFieldType, boolean userVisible) {
        this.dbFieldType = dbFieldType;
        this.userVisible = userVisible;
    }

    public String getDbFieldType() {
        return dbFieldType;
    }

    public static List<StorageDomain.FieldType> getAllFieldType() {
        PgFieldTypeEnum[] values = PgFieldTypeEnum.values();
        return Arrays.stream(values)
            .filter(i -> i.userVisible)
            .map(i -> new StorageDomain.FieldType(i.dbFieldType, i.name()))
            .collect(Collectors.toList());
    }

    /**
     * 主数据字段类型列表
     *
     * @return
     */
    public static List<String> getMdAllFieldType() {
        PgFieldTypeEnum[] values = PgFieldTypeEnum.values();
        return Arrays.stream(values).map(i -> i.dbFieldType).collect(Collectors.toList());
    }
}
